package alejandro.castro.extras;

import com.uqbar.vainilla.DeltaState;
import com.uqbar.vainilla.events.constants.Key;

import alejandro.castro.abstracts.AsteroidGameComponent;

public class MusicController extends AsteroidGameComponent {

	private MusicPlayerThread player;
	private boolean playing;

	public MusicController() {
		super(0, 0);
		play();
	}

	private void play() {
		playing = true;
		this.player = new MusicPlayerThread();
		this.player.start();
	}
	
	@Override
	public void update(DeltaState deltaState) {
		super.update(deltaState);
		if(deltaState.isKeyPressed(Key.P)){
			if(playing){
				stop();
			}
			else{
				play();
			}
		}
	}

	private void stop() {
		this.player.pause();
		this.player.interrupt();
		playing = false;
	}

}
